/*
 * @Author: your name
 * @Date: 2020-10-04 15:48:38
 * @LastEditTime: 2020-10-04 16:50:12
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \woniutaofang\lib\pages\mine\page\collection_page.dart
 */
import 'package:flutter/material.dart';
import 'package:woniutaofang/utils/colors.dart';
import 'package:woniutaofang/utils/styles.dart';

class CollectionPage extends StatefulWidget {
  CollectionPage({Key key}) : super(key: key);

  @override
  _CollectionPageState createState() => _CollectionPageState();
}

class _CollectionPageState extends State<CollectionPage>
    with SingleTickerProviderStateMixin {
  List<String> titleTabs = ['楼盘', "户型", "资讯"];
  TabController _controller;
  int customIndex = 0;

  var _scrollController = ScrollController();
  var _showBackTop = false;

  @override
  void initState() {
    super.initState();
    _controller = TabController(
        vsync: this, length: titleTabs.length, initialIndex: customIndex)
      ..addListener(() {
        setState(() {
          customIndex = _controller.index;
        });
      });
    // 对 scrollController 进行监听
    _scrollController.addListener(() {
      // _scrollController.position.pixels 获取当前滚动部件滚动的距离
      // 当滚动距离大于 800 之后，显示回到顶部按钮
      setState(() => _showBackTop = _scrollController.position.pixels >= 800);
    });
  }

  @override
  Widget build(BuildContext context) {
    return LightTheme(
      child: Scaffold(
          appBar: AppBar(
            // automaticallyImplyLeading: false,
            title: Text('我的收藏'),
            // backgroundColor: Colors.red,
            bottom: PreferredSize(
              child: TabBar(
                tabs: titleTabs.map((title) => Tab(text: "$title")).toList(),
                controller: _controller,
                indicatorColor: Colors.redAccent,
                labelColor: Colors.redAccent,
                unselectedLabelColor: Colors.black54,
                indicatorSize: TabBarIndicatorSize.label,
                labelStyle: TextStyle(fontSize: 16),
                unselectedLabelStyle: TextStyle(fontSize: 14),
              ),
              preferredSize: Size.fromHeight(38),
            ),
          ),
          body: SafeArea(
            top: false,
            child: TabBarView(
              controller: _controller,
              children: [
                _floorWidget(),
                _floorWidget2(),
                _floorWidget3(),
              ],
            ),
          ),
          floatingActionButton: _showBackTop // 当需要显示的时候展示按钮，不需要的时候隐藏，设置 null
              ? FloatingActionButton(
                  backgroundColor: Colors.red,
                  // mini: true,
                  onPressed: () {
                    // scrollController 通过 animateTo 方法滚动到某个具体高度
                    // duration 表示动画的时长，curve 表示动画的运行方式，flutter 在 Curves 提供了许多方式
                    _scrollController.animateTo(0.0,
                        duration: Duration(milliseconds: 300),
                        curve: Curves.decelerate);
                  },
                  child: Icon(
                    Icons.vertical_align_top,
                    color: Colors.white,
                  ),
                )
              : null),
    );
  }

  Widget _floorWidget() {
    print('楼盘收藏初始化了');
    return ListView.builder(
      controller: _scrollController,
      itemBuilder: (context, index) => Text('data$index'),
      itemCount: 100,
      physics: const BouncingScrollPhysics(),
      padding: const EdgeInsets.all(5.0),
    );
  }

  Widget _floorWidget2() {
    print('户型收藏初始化了');

    return Text('户型收藏');
  }

  Widget _floorWidget3() {
    print('资讯收藏初始化了');
    return Text('资讯收藏');
  }
}

class LightTheme extends StatelessWidget {
  final Widget child;

  LightTheme({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Theme(
        data: ThemeData(
            accentColor: readerMainColor,
            primaryColor: readerMainColor,
            scaffoldBackgroundColor: Colors.grey[200],
            appBarTheme: AppBarTheme(
                iconTheme: lightIconTheme,
                elevation: 0.0,
                brightness: Brightness.light,
                color: Colors.white)),
        child: child);
  }
}
